/*
 * This file is part of aion-engine <aion-engine.com>
 *
 * aion-engine is private software: you can redistribute it and or modify
 * it under the terms of the GNU Lesser Public License as published by
 * the Private Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * aion-engine is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser Public License for more details.
 *
 * You should have received a copy of the GNU Lesser Public License
 * along with aion-engine.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionengine.gameserver.model.challenge;

import com.aionengine.gameserver.dataholders.DataManager;
import com.aionengine.gameserver.model.gameobjects.PersistentState;
import com.aionengine.gameserver.model.templates.challenge.ChallengeQuestTemplate;
import com.aionengine.gameserver.model.templates.challenge.ChallengeTaskTemplate;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;


/**
 * @author ViAl
 */
public class ChallengeTask {

    private final int taskId;
    private final int ownerId;
    private Map<Integer, ChallengeQuest> quests;
    private Timestamp completeTime;
    private ChallengeTaskTemplate template;

    /**
     * Used for loading tasks from DAO.
     *
     * @param header
     * @param quests
     * @param completeTime
     */
    public ChallengeTask(int taskId, int ownerId, Map<Integer, ChallengeQuest> quests, Timestamp completeTime) {
        this.taskId = taskId;
        this.ownerId = ownerId;
        this.quests = quests;
        this.completeTime = completeTime;
        this.template = DataManager.CHALLENGE_DATA.getTaskByTaskId(taskId);
    }

    /**
     * Used for creating new tasks in runtime.
     *
     * @param ownerId
     * @param template
     */
    public ChallengeTask(int ownerId, ChallengeTaskTemplate template) {
        this.taskId = template.getId();
        this.ownerId = ownerId;
        Map<Integer, ChallengeQuest> quests = new HashMap<Integer, ChallengeQuest>();
        for (ChallengeQuestTemplate qt : template.getQuests()) {
            ChallengeQuest quest = new ChallengeQuest(qt, 0);
            quest.setPersistentState(PersistentState.NEW);
            quests.put(qt.getId(), quest);
        }
        this.quests = quests;
        this.completeTime = new Timestamp(1000);
        this.template = template;
    }

    public int getTaskId() {
        return this.taskId;
    }

    public int getOwnerId() {
        return this.ownerId;
    }

    public int getQuestsCount() {
        return quests.size();
    }

    public Map<Integer, ChallengeQuest> getQuests() {
        return quests;
    }

    public ChallengeQuest getQuest(int questId) {
        return quests.get(questId);
    }

    public Timestamp getCompleteTime() {
        return completeTime;
    }

    public synchronized void updateCompleteTime() {
        completeTime.setTime(System.currentTimeMillis());
    }

    public ChallengeTaskTemplate getTemplate() {
        return this.template;
    }

    public boolean isCompleted() {
        boolean isCompleted = true;
        for (ChallengeQuest quest : quests.values()) {
            if (quest.getCompleteCount() < quest.getMaxRepeats()) {
                isCompleted = false;
                break;
            }
        }
        return isCompleted;
    }
}